package com.ltf.service;

import com.ltf.dao.XUserDao;
import com.ltf.entity.Pag;
import com.ltf.entity.XUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class XUserServiceImpl implements XUserService {
    @Autowired(required = false)
    XUserDao xUserDao;
    @Autowired
    PasswordEncoder passwordEncoder;//用于添加和修改,密码加密

    public XUserServiceImpl(){}

    @Override
    public XUser findByPhone(String phone) {
        XUser user = this.xUserDao.findByPhone(phone);
        return user;
    }

    @Override
    public XUser findByUid(Integer uid) {
        return xUserDao.findByUid(uid);
    }

    @Override
    public List<XUser> findByRole(String role) {
        return this.xUserDao.findByRole(role);
    }

    @Override
    public List<XUser> findByGrade(Integer grade) {
        return this.xUserDao.findByGrade(grade);
    }

    @Override
    public List<XUser> findByPage(Map<String,Object>map) {
        List<XUser> stus = this.xUserDao.findByPage(map);
        return stus;
    }


    @Override
    public int findByCount(Map<String,Object> map) {
        return this.xUserDao.findByCount(map);
    }

    @Override
    public int del(Integer uid) {
        return this.xUserDao.del(uid);
    }

    @Override
    public int add(XUser xUser) {
        //把密码拿出来加密处理
        String pwd = xUser.getPwd();
        if (pwd!=null&&pwd.length()!=0){
            xUser.setPwd(this.passwordEncoder.encode(pwd));
        }
        return this.xUserDao.add(xUser);
    }

    @Override
    public int update(XUser xUser) {
        xUser.setPwd("111");
        String pwd = xUser.getPwd();
        if (pwd!=null&&pwd.length()!=0){
            xUser.setPwd(this.passwordEncoder.encode(pwd));
        }
        return this.xUserDao.update(xUser);
    }

    @Override
    public List<XUser> findByMax(String role) {
        return this.xUserDao.findByMax(role);
    }

    @Override
    public List<Integer> findGrade() {
        return this.xUserDao.findGrade();
    }

    @Override
    public int updatePwd(XUser xUser) {
        String pwd = xUser.getPwd();
        if (pwd != null && pwd.length() != 0) {
            xUser.setPwd(this.passwordEncoder.encode(pwd));
        } else {
            xUser.setPwd((String)null);
        }
        return this.xUserDao.update(xUser);
    }

    @Override
    public Map<String, Object> findBypag(Pag pag, Integer pagenum, Integer lines) {
        //空或小于最小的让其设默认值为第一页开始显示
        if (pagenum==null||pagenum<1) pagenum=1;
        //显示几行设个默认值
        if (lines==null||lines<1) lines=4;
        Map<String,Object> map=new HashMap<>();
        map.put("role","stu");
        map.put("pag",pag);
        map.put("pagenum",lines*(pagenum-1));
        map.put("lines",lines);
        List<XUser> stus = this.xUserDao.findByPage(map);
        //拿总数
        int count = this.xUserDao.findByCount(map);
        //将两个数据存到map再给到前端
        map.put("total",count);
        map.put("stus",stus);
        return map;
    }
}
